﻿using System;
using System.Collections.Generic;
using System.Text;
using Cabal.Kernel.Provider;
using Cabal.Kernel.Output;
using Cabal.ScriptEngine;
using System.Threading;

namespace Cabal.Kernel
{
    public class CabalSoul : IDisposable
    {
        public CabalSoul()
        {
            _support = new EnvSupport();
            _setting = new Setting(this, Setting.Path_Default);
            _resctrl = new BaseResControl(this);
            _rnd = new Random();
            _output_o = new BaseOutput(this);
            _const = new ConstProvider(this);

            //InitScriptEngine();
        }

        public CabalSoul(bool crypttest)
        {
            if (crypttest)
            {
                _support = new EnvSupport();
                _const = new ConstProvider(this);
            }
            else
            {
                _support = new EnvSupport();
                _setting = new Setting(this, Setting.Path_Default);
                _resctrl = new BaseResControl(this);
                _rnd = new Random();
                _output_o = new BaseOutput(this);
                _const = new ConstProvider(this);
            }
                

            //InitScriptEngine();
        }
        


        EnvSupport _support;
        Setting _setting;
        BaseResControl _resctrl;
        BaseOutput _output_o;
        MissionManager _output;
        ScriptEngine.ScriptEngine _engine;
        ConstProvider _const;

        Thread _thread;
        Random _rnd;
        

        public EnvSupport Support
        {
            get { return _support; }
            set { _support = value; }
        }
        public Setting Settings
        {
            get { return _setting; }
            //set { _setting = value; }
        }
        public BaseResControl ResControl
        {
            get { return _resctrl; }
        }
        public ScriptEngine.ScriptEngine CFSEngine
        {
            get { return _engine; }
        }
        public ConstProvider ConstControl
        {
            get { return _const; }
        }
        public Thread ScriptRunningThread
        {
            get { return _thread; }
        }
        public Random RandomGener
        {
            get { return _rnd; }
        }

        public BaseOutput OutputControl
        {
            get { return _output_o; }
        }
        public MissionManager OutputSupport
        {
            get { return _output; }
        }


        public void InitScriptEngine()
        {
            throw new NotImplementedException();

            //初始化部件
            _engine = ScriptEngine.ScriptEngine.CreateEngine();

            //装入函数
            //装入脚本

        }


        #region ProcessThread
        public bool StartWork()
        {
            throw new NotImplementedException();
            if (_thread.ThreadState != ThreadState.Stopped && _thread.ThreadState != ThreadState.Unstarted)
                return false;

            if (_thread.ThreadState == ThreadState.Stopped)
                initThread();

            _thread.Start();
        }
        public void StopWork()
        {
            if (_thread == null)
                return;
            switch (_thread.ThreadState)
            {
                case ThreadState.Running:
                    _thread.Abort();
                    break;
                case ThreadState.WaitSleepJoin:
                    _thread.Interrupt();
                    _thread.Abort();
                    break;
                default:
                    break;
            }
        }

        void initThread()
        {
            this._thread = new Thread(new ThreadStart(this.ThreadMain));
            _thread.Name = "ScriptRunningThread";
            _thread.IsBackground = true;
        }
        void ThreadMain()
        {
            Token t = new Token(TokenType.头部, CabalSoulConst.Token_Work);

            while (true)
            {
                CFSEngine.RunProgram(t);
            }
        }

        #endregion


        //#region Event

        //protected void OnGotMessage(MessageEventArgs e)
        //{
        //    GotMessage((object)this, e);
        //}

        //public event MessageEventHandler GotMessage;

        //#endregion

        #region IDisposable 成员

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class CabalSoulConst
    {
        public static string Token_Work = "工作循环";
    }



}
